package com.itlaobing.response.body;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Content-Disposition 响应头指示回复的内容该以何种形式展示，
 * 是以内联的形式（即网页或者页面的一部分），还是以附件的形式下载并保存到本地。
 * Content-Disposition: inline
 * Content-Disposition: attachment
 * Content-Disposition: attachment; filename="filename.jpg"
 */

// 下载 图片
@WebServlet( "/download/image/*" )
public class DownloadImageServlet extends HttpServlet{
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String path = "E:/";
        String name = "ayao.png";
        InputStream in = new FileInputStream(path + name);

        response.setHeader("content-type","image/png");

        //response.setHeader("content-Disposition","attachment");
        // 只 生成  名为  image 的 .file 文件
        response.setHeader("content-Disposition","attachment;filename="+name);

        OutputStream out = response.getOutputStream();

        in.transferTo(out);
    }

}
